import { useDictStoreWithOut } from '@/stores/modules/dict'
import { ElementPlusInfoType } from '@/types/elementPlus'

const dictStore = useDictStoreWithOut()

export interface DictDataType {
  dictType: string
  label: string
  value: any
  colorType: ElementPlusInfoType | 'default' | ''
  cssClass: string
}

export interface NumberDictDataType extends DictDataType {
  value: number
}

export interface BoolDictDataType extends DictDataType {
  value: boolean
}

export const getDictOptions = (dictType: string) => {
  return dictStore.getDictByType(dictType) || []
}

export const useDict = (...dictTypes: string[]): Record<any, DictDataType[]> => {
  if (!dictTypes?.length) {
    return {}
  }
  const res = {}
  dictTypes.forEach((dictType) => {
    res[dictType] = getDictOptions(dictType)
  })
  return res
}

export const useIntDict = (...dictTypes: string[]): Record<any, NumberDictDataType[]> => {
  if (!dictTypes?.length) {
    return {}
  }
  const res = {}
  dictTypes.forEach((dictType) => {
    const numDictOptions: NumberDictDataType[] = []
    const dictOptions: DictDataType[] = getDictOptions(dictType)
    dictOptions.forEach((dict) => {
      numDictOptions.push({
        ...dict,
        value: parseInt(dict.value + '')
      })
    })
    res[dictType] = numDictOptions
  })
  return res
}

export const useBoolDict = (...dictTypes: string[]): Record<any, BoolDictDataType[]> => {
  if (!dictTypes?.length) {
    return {}
  }
  const res = {}
  dictTypes.forEach((dictType) => {
    const boolDictOptions: BoolDictDataType[] = []
    const dictOptions: DictDataType[] = getDictOptions(dictType)
    dictOptions.forEach((dict) => {
      boolDictOptions.push({
        ...dict,
        value: dict.value + '' === 'true'
      })
    })
    res[dictType] = boolDictOptions
  })
  return res
}

export const getDictObj = (dictType: string, value: any): DictDataType | undefined => {
  const dictOptions: DictDataType[] = getDictOptions(dictType)
  for (const dict of dictOptions) {
    if (dict.value === value + '') {
      return dict
    }
  }
}

export const getDictLabel = (dictType: string, value: any): string => {
  const dictOptions: DictDataType[] = getDictOptions(dictType)
  for (const dict of dictOptions) {
    if (dict.value === value + '') {
      return dict.label
    }
  }
  // 这里是返回value的字符串值，还是空串
  return value + ''
}

/**
 * 使用枚举来定义字典类型值，这样具有强类型约束，限制值只能是这里定义的枚举值，而对象定义常量方式则没有这种约束
 * 对象定义常量方式：
 * export const DICT_TYPE {
 *  common_enable_status: 'common_enable_status',
 *  common_gender: 'common_gender'
 * }
 */
export enum DICT_TYPE {
  // =========== COMMON模块 ===========
  common_enable_status = 'common_enable_status',
  common_bool_type = 'common_bool_type',
  common_bool_flag = 'common_bool_flag',
  common_gender = 'common_gender',
  common_user_type = 'common_user_type',
  common_social_type = 'common_social_type',

  // =========== SYSTEM模块 ===========
  system_menu_type = 'system_menu_type',
  system_role_type = 'system_role_type',
  system_perm_data_scope = 'system_perm_data_scope',
  system_sms_channel_type = 'system_sms_channel_type',
  system_sms_business_type = 'system_sms_business_type',
  system_sms_send_status = 'system_sms_send_status',
  system_sms_receive_status = 'system_sms_receive_status',
  system_mail_send_status = 'system_mail_send_status',
  system_mail_template_type = 'system_mail_template_type',
  system_user_login_type = 'system_user_login_type',
  system_operate_log_module = 'system_operate_log_module',
  system_oauth2_grant_type = 'system_oauth2_grant_type',

  // =========== INFRA模块 ===========
  infra_file_category = 'infra_file_category',
  infra_codegen_scene = 'infra_codegen_scene',
  infra_codegen_template_type = 'infra_codegen_template_type',
  infra_codegen_front_type = 'infra_codegen_front_type',

  // =========== BPM模块 ===========
  bpm_model_form_type = 'bpm_model_form_type',
  bpm_process_listener_type = 'bpm_process_listener_type',
  bpm_process_listener_value_type = 'bpm_process_listener_value_type',
  bpm_process_instance_status = 'bpm_process_instance_status',
  bpm_task_status = 'bpm_task_status',
  bpm_oa_leave_type = 'bpm_oa_leave_type',

  // =========== AI模块 ===========
  ai_platform = 'ai_platform',
  ai_model_type = 'ai_model_type',
  ai_image_status = 'ai_image_status',
  ai_write_type = 'ai_write_type',
  ai_write_length = 'ai_write_length',
  ai_write_format = 'ai_write_format',
  ai_write_tone = 'ai_write_tone',
  ai_write_language = 'ai_write_language',

}
